package com.coocaa.ops.admin.v1.globalparams.converter;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.ops.admin.v1.globalparams.model.GlobalParamsModel;
import com.coocaa.ops.admin.v1.globalparams.model.GlobalParamsPageListModel;
import com.coocaa.ops.admin.v1.globalparams.model.ParamsInfoModel;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.biz.globalparams.entity.GlobalParamsContentEntity;
import com.coocaa.ops.common.core.biz.globalparams.entity.GlobalParamsEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author : hql
 * @date : 2020/7/15 15:29
 * @description
 */
public class GlobalParamsBeanConverter {


    public static GlobalParamsEntity convertGlobalParams(GlobalParamsModel model) {
        GlobalParamsEntity globalParamsEntity = new GlobalParamsEntity();
        globalParamsEntity.setId(model.getId());
        globalParamsEntity.setType(model.getType());
        globalParamsEntity.setComment(StringUtils.isBlank(model.getComment()) ? "" : model.getComment());
        globalParamsEntity.setName(model.getName());
        globalParamsEntity.setRemark(StringUtils.isBlank(model.getRemark()) ? "" : model.getRemark());
        globalParamsEntity.setSwitchStatus(model.getFlag() == null ? 1 : model.getFlag());
        List<ParamsInfoModel> paramsInfoModelList = model.getParamsInfo();
        if (CollectionUtils.isNotEmpty(paramsInfoModelList)) {
            List<GlobalParamsContentEntity> contentList = new LinkedList<>();
            for (ParamsInfoModel paramsInfoModel : paramsInfoModelList) {
                GlobalParamsContentEntity contentEntity = new GlobalParamsContentEntity();
                contentEntity.setDmpCrowdId(paramsInfoModel.getDmpCrowdId());
                contentEntity.setDmpCrowdName(paramsInfoModel.getDmpCrowdName());
                contentEntity.setDmpPolicyName(model.getDmpPolicyName());
                contentEntity.setDmpPolicyId(model.getDmpPolicyId());
                contentEntity.setParams(JSONObject.toJSONString(paramsInfoModel.getParams()));
                contentEntity.setGlobalParamsId(model.getId());
                contentList.add(contentEntity);
            }
            globalParamsEntity.setGlobalParamsContent(contentList);
        }
        return globalParamsEntity;
    }

    public static PageResultModel<GlobalParamsPageListModel> convertPageList(int total, List<GlobalParamsEntity> globalParamsList) {
        List<GlobalParamsPageListModel> pageListResult = InstanceUtil.newArrayList();
        for (GlobalParamsEntity entity : globalParamsList) {
            GlobalParamsPageListModel globalParamsModel = new GlobalParamsPageListModel(entity);
            List<GlobalParamsContentEntity> contentEntityList = entity.getGlobalParamsContent();
            if (CollectionUtils.isNotEmpty(contentEntityList)) {
                GlobalParamsContentEntity temp = contentEntityList.get(0);
                globalParamsModel.setDmpPolicyId(temp.getDmpPolicyId());
                globalParamsModel.setDmpPolicyName(temp.getDmpPolicyName());
                //contentList
                List<ParamsInfoModel> paramsInfoModelList = InstanceUtil.newArrayList();
                for (GlobalParamsContentEntity content : contentEntityList) {
                    ParamsInfoModel model;
                    if (StringUtils.isNotBlank(content.getParams())) {
                        Map paramsMap = JSONObject.parseObject(content.getParams(), Map.class);
                        model = new ParamsInfoModel(paramsMap, content.getDmpCrowdName(), content.getDmpCrowdId());
                    }else {
                        model = new ParamsInfoModel(content.getDmpCrowdName(), content.getDmpCrowdId());
                    }
                    paramsInfoModelList.add(model);
                }
                //设置content
                globalParamsModel.setParamsInfo(paramsInfoModelList);
            }
            pageListResult.add(globalParamsModel);
        }
        return PageResultModel.getPageResultModel(total, pageListResult);

    }
}
